
import { _decorator, Component, Node, tween } from 'cc';
import BasePanel from '../../../core/component/BasePanel';
import WindowManager from '../../../core/manager/WindowManager';
import WXTTUtils from '../../../core/manager/WXTTUtils';
import AudioMgr from '../../../core/utils/AudioMgr';
import { LevelClearLayer } from './LevelClearLayer';
const { ccclass, property } = _decorator;

@ccclass('DrawStarLayer')
export class DrawStarLayer extends BasePanel {

    indicator: Node = null;
    start() {
        this.indicator = this.GetGameObject("star-gamification-indicator");
        AudioMgr.Instance().playSFX('pass')
    }

    star = 0;
    setStar(s) {
        this.star = s;
        this.SetText("lbl_star", s + "");
    }

    dir = 1;
    beisu = 1;
    update() {
        if (this.pause) return;
        let pos = this.indicator.getPosition();
        pos.x += this.dir * 10;
        if (pos.x > 190) this.dir = -1;
        if (pos.x < -190) this.dir = 1;
        this.indicator.setPosition(pos);

        let x = this.indicator.getPosition().x;
        if (x >= -190 && x < -145) {
            this.beisu = 2;
        }
        else if (x >= -145 && x < -98) {
            this.beisu = 3;
        }
        else if (x >= -98 && x < -36) {
            this.beisu = 4;
        }
        else if (x >= -36 && x < 36) {
            this.beisu = 5;
        }
        else if (x >= 36 && x < 98) {
            this.beisu = 4;
        }
        else if (x >= 98 && x < 145) {
            this.beisu = 3;
        }
        else if (x >= 145 && x <= 190) {
            this.beisu = 2;
        }
        this.SetText("lbl_beisu", "x" + this.beisu);
    }

    pause = false;
    onBtnClicked(event, customEventData) {
        AudioMgr.Instance().playSFX('btnclick')
        var btnName = event.target.name;
        switch (btnName) {
            case "btn_get":
                this.pause = true;
                WindowManager.ins.open("LevelClearLayer").then((com: LevelClearLayer) => {
                    com.setAddStar(this.star);
                })
                this.close();
                break;
            case "btn_ad":
                this.pause = true;
                tween(this.node).delay(1).call(() => {
                    WXTTUtils.showVideo((b) => {
                        if (b) {
                            WindowManager.ins.open("LevelClearLayer").then((com: LevelClearLayer) => {
                                com.setAddStar(this.star * this.beisu);
                            })
                            this.close();
                        }
                    })
                }).start()
                break;
        }
    }
}
